#region File Description
//-----------------------------------------------------------------------------
// Game.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace GameStateManagement
{
    /// <summary>
    /// Sample showing how to manage different game states, with transitions
    /// between menu screens, a loading screen, the game itself, and a pause
    /// menu. This main game class is extremely simple: all the interesting
    /// stuff happens in the ScreenManager component.
    /// </summary>
    public class GameStateManagementGame : Microsoft.Xna.Framework.Game
    {


        GraphicsDeviceManager graphics;
        ScreenManager screenManager;
        public Utils.FrameRateCounter m_kFrameRate;
        public RenderTarget2D sceneRenderTarget;
        public RenderTarget2D normalRenderTarget;
        public Effect postProcessEffect;
        public SpriteBatch spriteBatch;
        public bool useNormalTarget; //when true draw to normalRenderTarget, when false draw to sceneRenderTarget
        public bool drawOutline;

        public float fTargetMsPerFrame
        {
            get { return (float)TargetElapsedTime.Milliseconds; }
            set
            {
                if (value > 1.0f)
                    TargetElapsedTime = System.TimeSpan.FromMilliseconds(value);
                else
                    TargetElapsedTime = System.TimeSpan.FromMilliseconds(1.0f);
            }
        }





        public GameStateManagementGame()
        {
            Content.RootDirectory = "Content";
            drawOutline = true;
            graphics = new GraphicsDeviceManager(this);

            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 1024;
            graphics.IsFullScreen = true;

            // Create the screen manager component.
            screenManager = new ScreenManager(this);

            Components.Add(screenManager);

            // Activate the first screens.
            screenManager.AddScreen(new BackgroundScreen());
            screenManager.AddScreen(new MainMenuScreen());

            m_kFrameRate = new Utils.FrameRateCounter(this, new Vector2(750.0f, 600.0f));
            Components.Add(m_kFrameRate);

            // For testing purposes, let's disable fixed time step and vsync.
            IsFixedTimeStep = true;
            graphics.SynchronizeWithVerticalRetrace = false;
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            ContentManager contentManager = new ContentManager(Services, "Content");

            PresentationParameters pp = GraphicsDevice.PresentationParameters;
            normalRenderTarget = new RenderTarget2D(GraphicsDevice,
                                    pp.BackBufferWidth, pp.BackBufferHeight, 1, pp.BackBufferFormat,
                                    pp.MultiSampleType, pp.MultiSampleQuality);

            sceneRenderTarget = new RenderTarget2D(GraphicsDevice,
                                    pp.BackBufferWidth, pp.BackBufferHeight, 1, pp.BackBufferFormat,
                                    pp.MultiSampleType, pp.MultiSampleQuality);

            postProcessEffect = contentManager.Load<Effect>("PostProcess");

            spriteBatch = new SpriteBatch(GraphicsDevice);
        }


        protected override void Draw(GameTime gameTime)
        {
            // graphics.GraphicsDevice.Clear(Color.Black);
            // The real drawing happens inside the screen manager component.
            if (GameplayScreen.isActivated)
            {
                useNormalTarget = true;
                GraphicsDevice.SetRenderTarget(0, this.normalRenderTarget);
                GraphicsDevice.Clear(Color.White);
                base.Draw(gameTime);

                useNormalTarget = false;
                GraphicsDevice.SetRenderTarget(0, this.sceneRenderTarget);
                GraphicsDevice.Clear(Color.Black);
                base.Draw(gameTime);
                GraphicsDevice.SetRenderTarget(0, null);


                ///////////////////
                EffectParameterCollection parameters = postProcessEffect.Parameters;

                Vector2 resolution = new Vector2(sceneRenderTarget.Width,

                sceneRenderTarget.Height);

                Texture2D normalDepthTexture = normalRenderTarget.GetTexture();

                parameters["edgeWidth"].SetValue(1.0f);

                parameters["edgeIntensity"].SetValue(1.0f);

                parameters["screenResolution"].SetValue(resolution);

                parameters["normalDepthTexture"].SetValue(normalDepthTexture);

                postProcessEffect.CurrentTechnique = postProcessEffect.Techniques["EdgeDetect"];

                spriteBatch.Begin(SpriteBlendMode.None,

                SpriteSortMode.Immediate,

                SaveStateMode.None);

                postProcessEffect.Begin();

                postProcessEffect.CurrentTechnique.Passes[0].Begin();

                spriteBatch.Draw(sceneRenderTarget.GetTexture(), Vector2.Zero, Color.White);

                spriteBatch.End();

                postProcessEffect.CurrentTechnique.Passes[0].End();

                postProcessEffect.End();
                //////////////////

                spriteBatch.Begin();
                spriteBatch.Draw(Content.Load<Texture2D>("hud"), new Vector2(880, 0), Color.White);
                if (GameplayScreen.shipLives > 0)
                {
                    spriteBatch.Draw(Content.Load<Texture2D>("lifeOne"), new Vector2(1225, 5), Color.White);
                }
                if (GameplayScreen.shipLives > 1)
                {
                    spriteBatch.Draw(Content.Load<Texture2D>("lifeTwo"), new Vector2(1205, 35), Color.White);
                }
                if (GameplayScreen.shipLives > 2)
                {
                    spriteBatch.Draw(Content.Load<Texture2D>("lifeThree"), new Vector2(1245, 35), Color.White);
                }
                drawScore();
                spriteBatch.End();

            }
            else
            {
                base.Draw(gameTime);
            }
        }

        public void drawScore()
        {
            long score = GameplayScreen.points;
            score -= score % 25;
            
            string temp = score.ToString();
            char[] numbers = temp.ToCharArray();
            Vector2 pos = new Vector2(980, 22);
            Vector2 offset = new Vector2(40, 0);
            for (int i = 0; i < numbers.Length; i++)
            {
                switch (numbers[i])
                {
                    case ('0'):
                        spriteBatch.Draw(Content.Load<Texture2D>("zero"), pos + (i * offset), Color.White);
                        break;
                    case ('1'):
                        spriteBatch.Draw(Content.Load<Texture2D>("one"), pos + (i * offset), Color.White);
                        break;
                    case ('2'):
                        spriteBatch.Draw(Content.Load<Texture2D>("two"), pos + (i * offset), Color.White);
                        break;
                    case ('3'):
                        spriteBatch.Draw(Content.Load<Texture2D>("three"), pos + (i * offset), Color.White);
                        break;
                    case ('4'):
                        spriteBatch.Draw(Content.Load<Texture2D>("four"), pos + (i * offset), Color.White);
                        break;
                    case ('5'):
                        spriteBatch.Draw(Content.Load<Texture2D>("five"), pos + (i * offset), Color.White);
                        break;
                    case ('6'):
                        spriteBatch.Draw(Content.Load<Texture2D>("six"), pos + (i * offset), Color.White);
                        break;
                    case ('7'):
                        spriteBatch.Draw(Content.Load<Texture2D>("seven"), pos + (i * offset), Color.White);
                        break;
                    case ('8'):
                        spriteBatch.Draw(Content.Load<Texture2D>("eight"), pos + (i * offset), Color.White);
                        break;
                    case ('9'):
                        spriteBatch.Draw(Content.Load<Texture2D>("nine"), pos + (i * offset), Color.White);
                        break;
                    default:
                        break;
                }

            }
        }


    }

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static class Program
    {
        static void Main()
        {
            using (GameStateManagementGame game = new GameStateManagementGame())
            {
                game.Run();
            }
        }
    }

}
